<template>
	<view>
		<view class="nodata" v-show="cart.length === 0">
			<image src="../../static/myicons/noData.svg" mode=""></image>
			<text>空空如也</text>
		</view>
		<view class="address" v-show="cart.length !== 0">
			<button v-show="JSON.stringify(address) === '{}'" type="default" @click="chooseAddress()" size="mini" style="background-color: #18BC37;color:#fff;">请选择收获地址</button>
			<view v-show="JSON.stringify(address) !== '{}'" class="address-info-box" @click="chooseAddress()">
				<view class="row1">
				  <view class="row1-left">
					<view class="username">收货人：<text>{{address.userName}}</text></view>
				  </view>
				  <view class="row1-right">
					<view class="phone">电话：<text>{{address.telNumber}}</text></view>
					<uni-icons type="arrowright" size="16"></uni-icons>
				  </view>
				</view>
				<view class="row2">
				  <view class="row2-left">收货地址：</view>
				  <view class="row2-right">{{addstr}}</view>
				</view>
			</view>
			<image src="../../static/myicons/cart_border@2x.png" mode=""></image>
		</view>
		<view class="content" v-show="cart.length !== 0">
			<view class="contentTop"><uni-icons type="shop" size="20"></uni-icons><text>购物车</text></view>
			<scroll-view scroll-y="true" :style="{height: wh + 'px'}">
				<uni-swipe-action>
					<!-- <view v-for="(item,i) in cart" :key="i" class="center"> -->
						<!-- 基础用法 -->
						<uni-swipe-action-item v-for="(item,i) in cart" :key="i" :right-options="options"  @click="swipeActionClickHandler(item)">
							<view class="center">
								<radio :checked="item.goods_state" style="margin-top: 25px;" color= '#c00' @click="itemChange(i)"/>
								<image :src="item.goods_small_logo" mode=""></image>
								<view class="centerRight">
									<text>{{item.goods_name}}</text>
									<view class="price">
										<text>￥{{item.goods_price}}.00</text>
										<!-- 商品数量 -->
										<uni-number-box :min="1" :value="item.goods_count" @click.native="handleNum1(item)" @change="handleNum"></uni-number-box>
									</view>
								</view>
							</view>
						</uni-swipe-action-item>
					<!-- </view> -->
				</uni-swipe-action>
			</scroll-view>
			
			<view class="payNow">
				<radio :checked="checkAll" color= '#c00' @click="handleChange"/> 全选
				<text class="allText">合计:<text>￥{{priceTotal}}.00</text></text>
				<button size="mini" type="default" style="background-color: #c00;color:#fff;">结算<text v-show="selectedNum !== 0">({{selectedNum}})</text></button>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex'
	import badgeMixin from '../../mixins/tabbar-badge.js'
	export default {
		data() {
			return {
				checkAll: false,
				wh: 0,
				currentGood: {},
				options:[{
				            text: '删除',
				            style: {
				                backgroundColor: '#c00'
				            }
				        }
			    ]
			};
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight - 201
		},
		mixins:[badgeMixin],
		computed: {
			...mapState('m_cart', ['cart']),
			...mapState('m_address', ['address']),
			selectedNum() {
				let n = 0
				this.cart.map(x => {
					if (x.goods_state === true) {
						n += 1
					}
				})
				return n
			},
			priceTotal() {
				let total = 0
				this.cart.map(x => {
					if (x.goods_state === true) {
						total += x.goods_count * x.goods_price
					}
				})
				return total
			},
			// 收货详细地址的计算属性
			  addstr() {
			    if (!this.address.provinceName) return ''
			
			    // 拼接 省，市，区，详细地址 的字符串并返回给用户
			    return this.address.provinceName + this.address.cityName + this.address.countyName + this.address.detailInfo
			  }
		},
		methods: {
			...mapMutations('m_cart', ['REFRESHCOUNT', 'DELGOODS']),
			...mapMutations('m_address', ['ADDRESS']),
			async chooseAddress() {
				const [err,succ] = await uni.chooseAddress().catch(err => err)
				if (err === null && succ.errMsg === 'chooseAddress:ok') {
					this.ADDRESS(succ)
				}
			},
			handleChange() {
				this.checkAll = !this.checkAll
				this.cart.map(val => {
					val.goods_state = this.checkAll
				})
			},
			itemChange(i) {
				this.cart[i].goods_state = !this.cart[i].goods_state
				let num = 0
				this.cart.map(x => {
					if (x.goods_state === true) {
						num += 1
					}
				})
				if (num === this.cart.length) {
					this.checkAll = true
					num = 0
				} else {
					this.checkAll = false
					num = 0
				}
			},
			handleNum1(item) {
				this.currentGood = item
			},
			handleNum(i) {
				const timer = setTimeout(() => {
					this.currentGood.goods_count = i
					this.REFRESHCOUNT(this.currentGood)
					clearTimeout(timer)
				},0)
			},
			swipeActionClickHandler(goods) {
				this.DELGOODS(goods)
			}
		}
	}
</script>

<style lang="scss">
.nodata {
	margin-top: 300rpx;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	image {
		width: 300rpx;
		height: 300rpx;
		margin-bottom: 40rpx;
	}
}
.address {
	position: relative;
	text-align: center;
	height: 200rpx;
	button {
		margin-top: 75rpx;
	}
	image {
		position: absolute;
		bottom: 0;
		left: 0;
		height: 8rpx;
		width: 100%;
		display: inline-block;
	}
}
.content {
	.contentTop {
		padding: 0 10rpx;
		height: 80rpx;
		line-height: 80rpx;
		border-bottom: 1px solid #f0f0f0;
		text {
			margin-left: 8rpx;
			vertical-align: top
		}
	}
	.center {
		display: flex;
		padding: 20rpx;
		border-bottom: 1px solid #f0f0f0;
		image {
			width: 160rpx;
			height: 160rpx;
			margin-right: 20rpx;
			border-radius: 8rpx;
		}
		.centerRight {
			flex: 1;
			font-size: 13px;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			.price {
				display: flex;
				justify-content: space-between;
				text {
					font-size: 16px;
					color: #c00;
				}
			}
		}
	}
	.payNow {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 0 20rpx;
		// height: 120rpx;
		line-height: 120rpx;
		.allText {
			margin-left: 120rpx;
			text {
				color: #c00;
			}
		}
		button {
			position: absolute;
			right: 0;
			top: 0;
			height: 120rpx;
			width: 260rpx;
			text-align: center;
			border-radius: 0;
			line-height: 120rpx;
			padding: 0 20rpx 0 0;
		}
	}
}
// 选择收货地址的盒子
.address-choose-box {
  height: 90px;
  display: flex;
  align-items: center;
  justify-content: center;
}

// 渲染收货信息的盒子
.address-info-box {
  font-size: 12px;
  height: 90px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 5px;

  // 第一行
  .row1 {
    display: flex;
    justify-content: space-between;

    .row1-right {
      display: flex;
      align-items: center;

      .phone {
        margin-right: 5px;
      }
    }
  }

  // 第二行
  .row2 {
    display: flex;
    align-items: center;
    margin-top: 10px;

    .row2-left {
      white-space: nowrap;
    }
  }
}
</style>
